
package com.living.player.ui.recently;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.living.R;
import com.living.player.ui.recently.album.RecentlyAlbumFragment;
import com.living.player.ui.recently.audio.RecentlyAudioFragment;
import com.md.livingwidget.CoreBaseActivity;
import com.md.livingwidget.indicator.SmartTabLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * 最近播放
 */
public class RecentlyPlayActivity extends CoreBaseActivity implements View.OnClickListener { // implements TracksFragment.ITrackHolder {
    private static final String TAG = "RecentlyPlayActivity";
    ViewPager videoViewPager;
    SmartTabLayout smartTab;

    List<Fragment> fragmentList = new ArrayList();
    RecentlyAlbumFragment playingFragment;
    RecentlyAudioFragment playInfoFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_recently);
        setToolbarTitle("最近播放");
        videoViewPager = findViewById(R.id.videoViewPager);
        smartTab = findViewById(R.id.smartTab);

        initData();
    }

    public void initData() {
        playingFragment = new RecentlyAlbumFragment();
        playInfoFragment = new RecentlyAudioFragment();
        fragmentList.add(playingFragment);
        fragmentList.add(playInfoFragment);
        VideoAdapter videoAdapter = new VideoAdapter(getSupportFragmentManager(), fragmentList);
        videoViewPager.setAdapter(videoAdapter);
        smartTab.setViewPager(videoViewPager);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    public class VideoAdapter extends FragmentPagerAdapter {
        List<Fragment> fragmentList = new ArrayList();

        public VideoAdapter(@NonNull FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return (position == 0 ? "专辑" : "单曲");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (playingFragment != null) {
            playingFragment.onActivityResult(requestCode, resultCode, data);
        }
        if (playInfoFragment != null) {
            playInfoFragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
